﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.pro2e.console.service.auth;
using com.pro2e.web.util.page;
using com.pro2e.console.core.util;
using System.Collections;
using com.pro2e.console.service.auth.vo;
using com.pro2e.console.service.msg.vo;

public partial class adm_UC14_UC14_7_FunctionPathSet : System.Web.UI.Page
{
    AuthService authService = new AuthService();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack) 
        {
            InitData();
        }
    }

    private void InitData()
    {
        IList<MenuFunc> topMenu =  authService.GetTopMenuFunc();

        UIHelper.AddDropDowListItem(ref ddlTopMenu, topMenu.GetEnumerator(),  "MenuFuncName","Id");

        if (topMenu != null && topMenu.Count > 0) 
        {            
            UIHelper.AddDropDowListItem(ref ddlSubMenu, topMenu[0].SubFuncs.GetEnumerator(),  "MenuFuncName","Id");

            fillData();
        }        
    }

    private void fillData()
    {     
        string selectedId = ddlSubMenu.SelectedValue;

        if(!string.IsNullOrEmpty(selectedId))
        {
            MenuFunc menufunc = authService.DaoGetVOById<MenuFunc>(int.Parse(selectedId));

            txtMainPath.Text = menufunc.MainPath;

            gvPath.DataSource = menufunc.FuncionPaths;
            gvPath.DataBind();
        }
    }

    protected void gvAuth_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string cmdName = e.CommandName;
        string cmdArg = e.CommandArgument.ToString();

        Control ctrl = ((Control)e.CommandSource);
        FunctionPath fp = authService.DaoGetVOById<FunctionPath>(int.Parse(cmdArg));

        switch (cmdName) 
        {
            case "MyUpdate":               
                fp.Path = UIHelper.FindTextBoxText(ref ctrl,"txtFPath");
                authService.DaoUpdate(fp);
                lblMsg.Text = MsgVO.UPDATE_OK;
                fillData();
                UserMenuFuncContainer.GetInstance().ResetAll();
                break;

            case "MyDel":
                fp.BelongMenuFunc.FuncionPaths.Remove(fp);
                authService.DaoDelete(fp);
                lblMsg.Text = MsgVO.DELETE_OK;
                fillData();
                UserMenuFuncContainer.GetInstance().ResetAll();
                break;
        }

    }
    protected void btnUpdate_Click(object sender, ImageClickEventArgs e)
    {
        MenuFunc menufunc = authService.DaoGetVOById<MenuFunc>(int.Parse(ddlSubMenu.SelectedValue));

        menufunc.MainPath = txtMainPath.Text.Trim();

        authService.DaoUpdate(menufunc);

        lblMsg.Text = MsgVO.UPDATE_OK;
        UserMenuFuncContainer.GetInstance().ResetAll();
    }
    protected void btnAdd_Click(object sender, ImageClickEventArgs e)
    {
        MenuFunc subMenu = authService.DaoGetVOById<MenuFunc>(int.Parse(ddlSubMenu.SelectedValue));

        if (subMenu.SubFuncs == null) 
        {
            subMenu.FuncionPaths = new List<FunctionPath>();
        }

        FunctionPath fph = new FunctionPath();
        fph.BelongMenuFunc = subMenu;
        fph.Path = txtPath.Text;
        authService.DaoInsert(fph);
        subMenu.FuncionPaths.Add(fph);
        authService.DaoUpdate(subMenu);

        lblMsg.Text = MsgVO.INSERT_OK;
        txtPath.Text = "";
        fillData();
        UserMenuFuncContainer.GetInstance().ResetAll();
    }
    protected void dllTopMenu_SelectedIndexChanged(object sender, EventArgs e)
    {
        MenuFunc topMenu = authService.DaoGetVOById<MenuFunc>(int.Parse(ddlTopMenu.SelectedValue));
        ddlSubMenu.Items.Clear();
        UIHelper.AddDropDowListItem(ref ddlSubMenu, topMenu.SubFuncs.GetEnumerator(), "MenuFuncName", "Id");
        fillData();
    }
    protected void ddlSubMenu_SelectedIndexChanged(object sender, EventArgs e)
    {
        fillData();
    }
}
